package com.wonder.netty;


import com.wonder.app.vo.StallVo;

public  class NettyConnectHelper {
    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    /**
     * 发起连接
     */
    public static void doConnect(){
        try {
            /**
             * 使用异步注解方式启动netty客户端服务
             */
            new BootNettyClient().connect(2000, "192.168.111.111");
        }catch (Exception exception){
            System.out.println("tcp连接异常");
        }
    }



    /**
     * 接收字节数据并转换为16进制字符串
     * @param by
     * @return
     */
    public  static String receiveHexToString(byte[] by) {
        try {
            String str = bytes2Str(by);
            str = str.toUpperCase();
            return str;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("接收字节数据并转为16进制字符串异常");
        }
        return null;
    }

    /**
     * 字节数组转换为16进制字符串
     * @param src
     * @return
     */
    public static String bytes2Str(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            if(i>0){
                stringBuilder.append(" ");
            }
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     *  字节转换为16进制字符
     * @param src
     * @return
     */
    public static String byte2Str(byte src){
        StringBuilder stringBuilder = new StringBuilder("");
        int v = src & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            stringBuilder.append(0);
        }
        stringBuilder.append(hv.toUpperCase());
        return stringBuilder.toString();
    }

    /**
     * 接收字节数据并转换为对象
     * @param by
     * @return
     */
    public static StallVo receiveHexToObj(byte[] by) {
        try {
            StallVo stallVo = bytes2Obj(by);
            return stallVo;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("接收字节数据并转为对象异常");
        }
        return null;
    }

    /**
     * 对象属性赋值
     * @param src
     * @return
     */
    public static StallVo bytes2Obj(byte[] src){
        if (src == null || src.length <= 0) {
            return null;
        }
        //依据约定，第一位为区域编号；第四位为车辆状态；第五位为车辆编号
        StallVo stallVo = StallVo.builder()
                .areaNumber(byte2Str(src[0]))
                .carStatus(byte2Str(src[3]))
                .carNumber(byte2Str(src[4]))
                .build();
        return stallVo;
    }
    /**
     * 方法一：
     * 16进制byte转String
     *
     * @param bytes
     * @return
     */
    public static String bytesToHexFun(byte[] bytes) {
        // 一个byte为8位，可用两个十六进制位标识
        char[] buf = new char[bytes.length * 2];
        int a = 0;
        int index = 0;
        for(byte b : bytes) { // 使用除与取余进行转换
            if(b < 0) {
                a = 256 + b;
            } else {
                a = b;
            }

            buf[index++] = HEX_CHAR[a / 16];
            buf[index++] = HEX_CHAR[a % 16];
        }

        return new String(buf);
    }
}